package chat.aylan.top.wind;
/**
 * 聊天窗口
 */

import chat.aylan.top.commons.Information;

import javax.swing.*;
import javax.swing.border.MatteBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ChatWind extends JFrame {

    JTextArea content;
    JTextPane displayPanel;
    JButton sendBtn;
    private int userId;


    public ChatWind(int width,int height,int userId){
        this.setSize(width,height);
        this.userId = userId;
        JPanel displayArea = new JPanel();

        displayArea.setLayout(null);
        displayPanel = new JTextPane();
        displayPanel.setEditable(false);

        JScrollPane scrollPane = new JScrollPane(displayPanel);
        scrollPane.setBounds(10,10,460,200);
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);

        displayArea.add(scrollPane);

        JPanel bottom = new JPanel();
        JPanel box = new JPanel();

        content = new JTextArea(3,30);
        content.setLineWrap(true);
        content.setOpaque(true);
        content.setBorder(new MatteBorder(1, 1, 1, 1, new Color(122, 122, 122)));
        box.add(content);
        bottom.add(box,BorderLayout.CENTER);

        sendBtn = new JButton("发送");
        JPanel sendBtnBox = new JPanel();
        sendBtnBox.add(sendBtn);
        bottom.add(sendBtnBox,BorderLayout.EAST);

        this.setResizable(false);
        this.add(displayArea,BorderLayout.CENTER);
        this.add(bottom,BorderLayout.SOUTH);
        this.setLocationRelativeTo(null);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                ChatWind.this.setVisible(false);
            }
        });
        play();
    }

    //  开始工作 -- 初始化
    private void play(){
        sendBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String input = content.getText();
                if(null != input && !input.equals("")){
                    if(userId == 10001){
                        sendMsg(5,input);
                    }else{
                        sendMsg(2,input);
                    }
                    updatePane("  Me :   ",input);
                    content.setText("");
                }

            }
        });

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
               System.out.println("我下线了");
            }
        });
    }


    /**
     *  发送消息 ，判断群消息 或普通消息
     * @param type
     * @param input
     */
    public void sendMsg(int type ,String input){
        try {
            Information information = new Information(type);
            if(type == 2){
               information.put("message",input);
               information.put("toUserId",userId);
            }else if(type == 5){
                information.put("message",input);
            }
            ManagerWind.sender.send(information);
        } catch (IOException e) {
            //  e.printStackTrace();
        }
    }

    /**
     * 更新聊天界面
     * @param sender
     * @param input
     */
    public void updatePane(String sender,String input){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
        String text = sender+simpleDateFormat.format(date);
        text = text + "\n"+input+"\n";
        String org = displayPanel.getText();
        if(null == org || org.equals("")){
            displayPanel.setText(text);
        }else{
            displayPanel.setText(displayPanel.getText()+"\n"+text);
        }
        ChatWind.this.revalidate();
    }


    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }
}
